import 'package:flutter/material.dart';
import 'package:study_flutter/yzhg/dartTools/ToastTools.dart';
import 'package:study_flutter/yzhg/dartTools/YSPTools.dart';

class YSharedPreferences extends StatelessWidget {
  TextEditingController controller1 = TextEditingController();
  TextEditingController controller2 = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("SP保存数据")),
      body: Column(
        children: <Widget>[
          _inputView(controller1, Icon(Icons.vpn_key, size: 16.0),
              TextInputType.text, "输入SPKey"),
          _inputView(controller2, Icon(Icons.person, size: 16.0),
              TextInputType.text, "输入储存的数据"),
          _buttonView(() {
            var key = controller1.text;
            var value = controller2.text;
            if (key.isEmpty || value.isEmpty) {
              ToastTools.showToast("请输入key和value");
            } else {
              YSPTools.getInstance().putString(key, value);
              ToastTools.showToast("储存成功");
            }
          }, "点击保存"),
          _buttonView(() {
            var key = controller1.text;
            if (key.isEmpty) {
              ToastTools.showToast("请输入key");
            } else {
              YSPTools.getInstance().getString(key).then((value) {
                ToastTools.showToast("获取字符串${value.toString()}");
              });
            }
          }, "获取保存的字符串")
        ],
      ),
    );
  }

  Widget _inputView(TextEditingController controller, Icon icons,
      TextInputType keyboardType, String hintText) {
    return Padding(
      padding: EdgeInsets.all(20.0),
      child: DecoratedBox(
        decoration: BoxDecoration(
          //设置背景色为白色
          color: Colors.white,
          //设置边框
          border: Border.all(
              color: Colors.blueAccent, style: BorderStyle.solid, width: 1.0),
          //设置边框圆角
          borderRadius: BorderRadius.all(Radius.circular(30)),
        ),
        child: TextField(
          autofocus: false,
          //设置弹出数字键盘
          keyboardType: keyboardType,
          controller: controller,
          textAlign: TextAlign.left,
          style: TextStyle(fontSize: 14.0),
          decoration: InputDecoration(
              //去除下划线
              border: InputBorder.none,
              // labelText: '手机号',
              hintText: hintText,
              prefixIcon: icons),
        ),
      ),
    );
  }

  Widget _buttonView(VoidCallback callback, String butMsg) {
    return Padding(
        padding: EdgeInsets.fromLTRB(40.0, 0.0, 40.0, 0.0),
        child: Container(
          width: double.infinity,
          child: FlatButton(
              onPressed: callback,
              color: Colors.blue,
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(20.0)),
              child: Text(
                butMsg,
                style: TextStyle(fontSize: 14.01, color: Colors.white),
              )),
        ));
  }
}
